package ch.bfh.abcmmh.view.controlview;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import ch.bfh.abcmmh.controller.operating.LogicController;
import ch.bfh.abcmmh.view.GUIController;

public class ControlPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private GUIController gctrl;
	private LogicController lctrl;
	private LayoutHelper layoutHelper;
	private JLabel carCount, busCount, truckCount, vehicleCount, avgSpeed, calcCycles, vehiclesPerSec;
	private JButton btuCont, btuOneStep, btuFiveStep, btuBreak;

	public ControlPanel(GUIController gctrl) {
		this.gctrl = gctrl;
		this.lctrl = gctrl.getLogicController();
		Dimension size = new Dimension(gctrl.getWidth(), gctrl.getControlStreetPanelHeight());
		this.setPreferredSize(size);
		this.setLayout(new BorderLayout());
		this.add(new MenuPanel(), BorderLayout.NORTH);
		this.add(new OutputPanel(), BorderLayout.CENTER);
	}

	public void repaintMe() {
		layoutHelper.repaint();
	}

	class MenuPanel extends JPanel {

		private static final long serialVersionUID = 1L;

		public MenuPanel() {
			this.setLayout(new BorderLayout());
			JToolBar bar = new JToolBar();
			Dimension dim = new Dimension(gctrl.getWidth(), 33);
			bar.setPreferredSize(dim);
			bar.setFloatable(false);
			ImageIcon play = new ImageIcon(getClass().getResource("/play.gif"));
			btuCont = new JButton(play);
			btuCont.setFocusable(false);
			btuCont.setEnabled(true);
			btuCont.addActionListener(new BtuContListener());
			ImageIcon stepone = new ImageIcon(getClass().getResource("/step_one.gif"));
			btuOneStep = new JButton(stepone);
			btuOneStep.setFocusable(false);
			btuOneStep.setEnabled(true);
			btuOneStep.addActionListener(new BtuOneStepListener());
			ImageIcon fivestep = new ImageIcon(getClass().getResource("/step_five.gif"));
			btuFiveStep = new JButton(fivestep);
			btuFiveStep.setFocusable(false);
			btuFiveStep.setEnabled(true);
			btuFiveStep.addActionListener(new BtuFiveStepListener());
			ImageIcon pause = new ImageIcon(getClass().getResource("/break.gif"));
			btuBreak = new JButton(pause);
			btuBreak.setFocusable(false);
			btuBreak.setEnabled(false);
			btuBreak.addActionListener(new BtuBreakListener());
			bar.add(btuCont);
			bar.add(btuOneStep);
			bar.add(btuFiveStep);
			bar.add(btuBreak);
			this.add(bar, BorderLayout.LINE_START);
		}
	}

	class OutputPanel extends JPanel {

		private static final long serialVersionUID = 1L;

		public OutputPanel() {
			layoutHelper = new LayoutHelper();
			this.setLayout(new BorderLayout());
			this.add(layoutHelper, BorderLayout.LINE_START);
			setCarCount(0);
			setBusCount(0);
			setTruckCount(0);
			setVehicleCount(0);
			setAvgSpeed(0);
			setCalcCycles(0);
			setVehiclesPerSec(0);
		}
	}

	class BtuContListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			lctrl.startSimulation();
			btuCont.setEnabled(false);
			btuOneStep.setEnabled(true);
			btuFiveStep.setEnabled(true);
			btuBreak.setEnabled(true);
		}
	}

	class BtuOneStepListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			lctrl.oneStepSimulation();
			btuCont.setEnabled(true);
			btuOneStep.setEnabled(true);
			btuFiveStep.setEnabled(true);
			btuBreak.setEnabled(false);
		}
	}

	class BtuFiveStepListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			lctrl.fiveStepSimulation();
			btuCont.setEnabled(true);
			btuOneStep.setEnabled(true);
			btuFiveStep.setEnabled(true);
			btuBreak.setEnabled(false);
		}
	}

	class BtuBreakListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			lctrl.stopSimulation();
			btuCont.setEnabled(true);
			btuOneStep.setEnabled(true);
			btuFiveStep.setEnabled(true);
			btuBreak.setEnabled(false);
		}
	}

	public void setCarCount(int c) {
		this.carCount.setText("" + c);
	}

	public void setBusCount(int c) {
		this.busCount.setText("" + c);
	}

	public void setTruckCount(int c) {
		this.truckCount.setText("" + c);
	}

	public void setVehicleCount(int c) {
		this.vehicleCount.setText("" + c);
	}

	public void setAvgSpeed(int c) {
		this.avgSpeed.setText("" + c + " km/h");
	}

	public void setCalcCycles(int c) {
		this.calcCycles.setText("" + c);
	}

	public void setVehiclesPerSec(float c) {
		CharSequence d = String.valueOf(c);
		if (d.length() == 3) {
			d = d + "00";
		}
		if (d.length() == 4) {
			d = d + "0";
		}
		if (d.length() > 5) {
			d = d.subSequence(0, 5);
		}
		this.vehiclesPerSec.setText("" + d);
	}

	private class LayoutHelper extends JPanel {

		private static final long serialVersionUID = 1L;

		private LayoutHelper() {
			this.setLayout(new GridLayout(3, 3));
			this.add(new JLabel("  Number of cars:    "));
			this.add(carCount = new JLabel());
			this.add(new JLabel("  Total of vehicles:    "));
			this.add(vehicleCount = new JLabel());
			this.add(new JLabel("  Total cars per second:    "));
			this.add(vehiclesPerSec = new JLabel());
			this.add(new JLabel("  Number of buses:    "));
			this.add(busCount = new JLabel());
			this.add(new JLabel("  Average speed:    "));
			this.add(avgSpeed = new JLabel());
			this.add(new JLabel(""));
			this.add(new JLabel(""));
			this.add(new JLabel("  Number of trucks:    "));
			this.add(truckCount = new JLabel());
			this.add(new JLabel("  Calculation cycles:    "));
			this.add(calcCycles = new JLabel());
			this.add(new JLabel(""));
			this.add(new JLabel(""));
		}
	}
}